package de.preamp.clients.java.lib.ui;

import java.awt.BorderLayout;
import java.io.IOException;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;

import org.json.JSONException;

import de.preamp.clients.java.data.Album;
import de.preamp.clients.java.data.Artist;
import de.preamp.clients.java.data.Track;
import de.preamp.clients.java.net.ServerConnectionImpl;

/**
 * @author André Stannek
 * @version 2010-03-23
 * @deprecated not working since refactoring
 */
@Deprecated
public class LibraryUIDummy extends AbstractLibraryUI implements
	TreeSelectionListener
{

    /**
     * Generated UID.
     */
    private static final long serialVersionUID = -4395349590482829359L;

    /**
     * 
     */
    private JTree tree;

    /**
     */
    public LibraryUIDummy()
    {
	super();
	initUI();
    }

    /**
     * initiates UI components.
     */
    private void initUI()
    {
	final DefaultMutableTreeNode top =
		new DefaultMutableTreeNode("Ugly Dummy Library");
	createNodes(top);
	tree = new JTree(top);
	tree.getSelectionModel().setSelectionMode(
		TreeSelectionModel.SINGLE_TREE_SELECTION);
	tree.addTreeSelectionListener(this);
	final JScrollPane treeView = new JScrollPane(tree);
	this.setLayout(new BorderLayout());
	this.add(treeView, BorderLayout.CENTER);

    }

    /**
     * Creates the complete tree under a root node.
     * 
     * @param top
     *            the root node of the tree.
     */
    private void createNodes(final DefaultMutableTreeNode top)
    {
	DefaultMutableTreeNode artNode = null;
	DefaultMutableTreeNode albumNode = null;
	DefaultMutableTreeNode trackNode = null;
	SortedSet<Artist> artists = null;
	try
	{
	    artists =
		    new TreeSet<Artist>(
			    (new ServerConnectionImpl()).getAllArtists());
	    final int howMany = 10;
	    Artist artist;
	    final Iterator<Artist> iterator = artists.iterator();

	    for (int i = 0; i < howMany; i++)
	    {
		artist = iterator.next();
		artNode = new DefaultMutableTreeNode(artist);
		top.add(artNode);
		for (final Album album : artist.getAlbums())
		{
		    albumNode = new DefaultMutableTreeNode(album);
		    artNode.add(albumNode);
		    for (final Track track : album.getTracks())
		    {
			trackNode = new DefaultMutableTreeNode(track);
			albumNode.add(trackNode);
		    }
		}
	    }
	}
	catch (final JSONException e)
	{
	    e.printStackTrace();
	}
	catch (final IOException e)
	{
	    e.printStackTrace();
	}

    }

    @Override
    public final void valueChanged(final TreeSelectionEvent e)
    {
	final DefaultMutableTreeNode node =
		(DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

	if (node == null)
	{
	    // Nothing is selected.
	    return;
	}

	final Object nodeInfo = node.getUserObject();
	if (node.isLeaf())
	{
	    // FIXME DummyLib not working with player anymore
	    @SuppressWarnings("unused")
	    final Track track = (Track) nodeInfo;
	    // PreampPlayer player = new PreampPlayer(track);
	    // player.run();
	}

    }

}
